package com.example.demo.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.example.demo.mapper.UserMapper;
import com.example.demo.entity.User;
import com.example.demo.entity.UserHolder;
import com.example.demo.util.IPUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
public class UserServiceI  implements UserDetailsService {

    @Autowired
    private  UserMapper um;

    @Resource
    private  StringRedisTemplate stringRedisTemplate;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (username == null) throw new UsernameNotFoundException("用户名不能为空");
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String inputCode=(String)request.getAttribute("code");
        Integer identity=Integer.valueOf(request.getParameter("identity"));
        String code = (String) request.getSession().getAttribute("index_code");

        User user = um.findByname(username);

        if (user == null) throw new UsernameNotFoundException("用户名不存在");
        um.updateUserIPAndTime(IPUtil.getClientIpAddress(request), user.getId());
        UserHolder.saveUser(user);
        Map<String, Object> usermap = BeanUtil.beanToMap(user,new HashMap<>(), CopyOptions.create()
                .setIgnoreNullValue(true)
                .setFieldValueEditor((fieldName,fieldValue)->fieldValue==null ? "" : fieldValue.toString()));
        String key="user:"+user.getId();
        stringRedisTemplate.opsForHash().putAll(key,usermap);
        stringRedisTemplate.expire("user:"+user.getId(),30, TimeUnit.MINUTES);

        request.getSession().setAttribute("user",user);
        if(user.getRole()==1&&identity==1)
        return org.springframework.security.core.userdetails.User.withUsername(user.getName())
                .password(user.getPwd())
                .roles("admin").build();
     if(user.getRole()==2&&identity==2)
            return org.springframework.security.core.userdetails.User.withUsername(user.getName())
                    .password(user.getPwd())
                    .roles("teacher").build();
    if(user.getRole()==3&&identity==3)return org.springframework.security.core.userdetails.User.withUsername(user.getName())
                    .password(user.getPwd())
                    .roles("user").build();

    else throw new IllegalArgumentException("User role is error");
    }


}
